{**********************************************************************}
{                                                                      }
{                           Settings unit                              }
{         programming                                                  }
{         presto.development@gmail.com                                 }
{         Miroslav Koretskiy                                           }
{**********************************************************************}

unit uSettings;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, StdCtrls, ExtCtrls, Mask, JvExMask, JvSpin,
  JvComCtrls, DB, ADODB, IniFiles, JvToolEdit,
  GridsEh, DBGridEh, DBGridEhGrouping;

type
  TfmSettings = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet3: TTabSheet;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Memo1: TMemo;
    Label7: TLabel;
    Label8: TLabel;
    Label9: TLabel;
    Label10: TLabel;
    grp1: TGroupBox;
    grp2: TGroupBox;
    chk4: TCheckBox;
    lbl1: TLabel;
    JvFilenameEdit1: TJvFilenameEdit;
    btn3: TButton;
    chk1: TCheckBox;
    chk3: TCheckBox;
    ts1: TTabSheet;
    DBGridEh1: TDBGridEh;
    Bevel1: TBevel;
    lbl2: TLabel;
    edt1: TEdit;
    chk20: TCheckBox;
    lbl3: TLabel;
    edt2: TEdit;
    chk2: TCheckBox;
    procedure FormShow(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure chk4Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
    IniFile : TIniFile;
  end;

var
  fmSettings: TfmSettings;

implementation

uses AutoRun, uMain, EbayConnect, Registry;

{$R *.dfm}

procedure AddEntryToRegistry;
var key: string;
     Reg: TRegIniFile;
begin
  key := '\Software\Microsoft\Windows\CurrentVersion\Run';
  Reg := TRegIniFile.Create;
  try
    Reg.RootKey:=HKEY_LOCAL_MACHINE;
    Reg.CreateKey(Key);
    if Reg.OpenKey(Key,False) then Reg.WriteString(key, 'Ebay Search tool', ParamStr(0));
  finally
    Reg.Free;
  end;
end;

procedure RemoveEntryFromRegistry;
var key: string;
     Reg: TRegIniFile;
begin
  key := '\Software\Microsoft\Windows\CurrentVersion\Run';
  Reg:=TRegIniFile.Create;
try
  Reg.RootKey:=HKey_Local_Machine;
  if Reg.OpenKey(Key,False) then Reg.DeleteValue('Ebay Search tool');
  finally
  Reg.Free;
  end;
end;

procedure TfmSettings.FormShow(Sender: TObject);
var i: Integer;
begin
  try
    IniFile := TIniFile.Create(fmMain.BasePath+'/settings.ini');
    // database settings
    JvFilenameEdit1.Text := IniFile.ReadString('database','DataSource',fmMain.BasePath + '\Data\finditems.mdb');
    chk20.Checked := IniFile.ReadBool('database','ClearData',True);
    // sheduling
    chk4.Checked := IniFile.ReadBool('sheduling','autorun',True);
    //chk1.Checked := IniFile.ReadBool('sheduling','autorunsearch',True);
    chk3.Checked := IniFile.ReadBool('sheduling','runminimized',True);
    chk1.Checked := IniFile.ReadBool('sheduling','checkregular',True);
    chk2.Checked := IniFile.ReadBool('debug','xmloutput',false);
    // ebay settings
    Edit2.Text := IniFile.ReadString('ebay','AppID','');
    Edit3.Text := IniFile.ReadString('ebay','DevID','');
    Edit4.Text := IniFile.ReadString('ebay','CertID','');
    Memo1.Text := IniFile.ReadString('ebay','Token','');
    edt1.Text := IniFile.ReadString('regional','DecimalSeparator',fmMain.GetLocaleInformation(LOCALE_SDECIMAL));
    edt2.Text := IniFile.ReadString('database','CSVSeparator',';');
  finally
    IniFile.Free;
  end;
  if not fmMain.tblSites.Active then fmMain.tblSites.Open;
end;

procedure TfmSettings.Button1Click(Sender: TObject);
var tmpstring: string;
    i: Integer;
    Autorun: TAutoRun;
begin
  try
    IniFile := TIniFile.Create(fmMain.BasePath+'/settings.ini');
    // database settings
    IniFile.WriteString('database','DataSource',JvFilenameEdit1.Text);
    IniFile.WriteBool('database','ClearData',chk20.Checked);
    // sheduling
    IniFile.WriteBool('sheduling','autorun',chk4.Checked);
    IniFile.WriteBool('sheduling','checkregular',chk1.Checked);
    //IniFile.WriteBool('sheduling','autorunsearch',chk1.Checked);
    IniFile.WriteBool('sheduling','runminimized',chk3.Checked);
    IniFile.WriteBool('debug','xmloutput',chk2.Checked);

    // ebay settings
    IniFile.WriteString('ebay','AppID',Edit2.Text);
    IniFile.WriteString('ebay','DevID',Edit3.Text);
    IniFile.WriteString('ebay','CertID',Edit4.Text);
    IniFile.WriteString('ebay','Token',Memo1.Text);
    IniFile.WriteString('regional','DecimalSeparator',edt1.Text);
    IniFile.WriteString('database','CSVSeparator',edt2.Text);
  finally
    IniFile.Free;
  end;
  if DBGridEh1.DataSource.State in [dsEdit] then  DBGridEh1.DataSource.DataSet.Post;
end;

procedure TfmSettings.Button2Click(Sender: TObject);
begin
  Close;
end;

procedure TfmSettings.chk4Click(Sender: TObject);
begin
  // autorun
  if chk4.Checked then AddEntryToRegistry else RemoveEntryFromRegistry;
end;

end.

(*
LOCALE_NOUSEROVERRIDE { do not use user overrides }
LOCALE_USE_CP_ACP { use the system ACP }
LOCALE_ILANGUAGE { language id }
LOCALE_SLANGUAGE { localized name of language }
LOCALE_SENGLANGUAGE { English name of language }
LOCALE_SABBREVLANGNAME { abbreviated language name }
LOCALE_SNATIVELANGNAME { native name of language }
LOCALE_ICOUNTRY { country code } 
LOCALE_SCOUNTRY { localized name of country } 
LOCALE_SENGCOUNTRY { English name of country } 
LOCALE_SABBREVCTRYNAME { abbreviated country name } 
LOCALE_SNATIVECTRYNAME { native name of country } 
LOCALE_IDEFAULTLANGUAGE { default language id } 
LOCALE_IDEFAULTCOUNTRY { default country code } 
LOCALE_IDEFAULTCODEPAGE { default oem code page } 
LOCALE_IDEFAULTANSICODEPAGE { default ansi code page } 
LOCALE_IDEFAULTMACCODEPAGE { default mac code page } 
LOCALE_SLIST { list item separator } 
LOCALE_IMEASURE { 0 = metric, 1 = US } 
LOCALE_SDECIMAL { decimal separator } 
LOCALE_STHOUSAND { thousand separator } 
LOCALE_SGROUPING { digit grouping } 
LOCALE_IDIGITS { number of fractional digits } 
LOCALE_ILZERO { leading zeros for decimal } 
LOCALE_INEGNUMBER { negative number mode } 
LOCALE_SNATIVEDIGITS { native ascii 0-9 } 
LOCALE_SCURRENCY { local monetary symbol } 
LOCALE_SINTLSYMBOL { intl monetary symbol } 
LOCALE_SMONDECIMALSEP { monetary decimal separator } 
LOCALE_SMONTHOUSANDSEP { monetary thousand separator } 
LOCALE_SMONGROUPING { monetary grouping } 
LOCALE_ICURRDIGITS { # local monetary digits } 
LOCALE_IINTLCURRDIGITS { # intl monetary digits } 
LOCALE_ICURRENCY { positive currency mode } 
LOCALE_INEGCURR { negative currency mode } 
LOCALE_SDATE { date separator }
LOCALE_STIME { time separator } 
LOCALE_SSHORTDATE { short date format string } 
LOCALE_SLONGDATE { long date format string } 
LOCALE_STIMEFORMAT { time format string } 
LOCALE_IDATE { short date format ordering } 
LOCALE_ILDATE { long date format ordering } 
LOCALE_ITIME { time format specifier } 
LOCALE_ITIMEMARKPOSN { time marker position } 
LOCALE_ICENTURY { century format specifier (short date) } 
LOCALE_ITLZERO { leading zeros in time field } 
LOCALE_IDAYLZERO { leading zeros in day field (short date) } 
LOCALE_IMONLZERO { leading zeros in month field (short date) } 
LOCALE_S1159 { AM designator } 
LOCALE_S2359 { PM designator } 
LOCALE_ICALENDARTYPE { type of calendar specifier } 
LOCALE_IOPTIONALCALENDAR { additional calendar types specifier } 
LOCALE_IFIRSTDAYOFWEEK { first day of week specifier } 
LOCALE_IFIRSTWEEKOFYEAR { first week of year specifier } 
LOCALE_SDAYNAME1 { long name for Monday } 
LOCALE_SDAYNAME2 { long name for Tuesday } 
LOCALE_SDAYNAME3 { long name for Wednesday } 
LOCALE_SDAYNAME4 { long name for Thursday } 
LOCALE_SDAYNAME5 { long name for Friday } 
LOCALE_SDAYNAME6 { long name for Saturday } 
LOCALE_SDAYNAME7 { long name for Sunday } 
LOCALE_SABBREVDAYNAME1 { abbreviated name for Monday } 
LOCALE_SABBREVDAYNAME2 { abbreviated name for Tuesday } 
LOCALE_SABBREVDAYNAME3 { abbreviated name for Wednesday } 
LOCALE_SABBREVDAYNAME4 { abbreviated name for Thursday } 
LOCALE_SABBREVDAYNAME5 { abbreviated name for Friday } 
LOCALE_SABBREVDAYNAME6 { abbreviated name for Saturday }
LOCALE_SABBREVDAYNAME7 { abbreviated name for Sunday } 
LOCALE_SMONTHNAME1 { long name for January } 
LOCALE_SMONTHNAME2 { long name for February } 
LOCALE_SMONTHNAME3 { long name for March } 
LOCALE_SMONTHNAME4 { long name for April } 
LOCALE_SMONTHNAME5 { long name for May } 
LOCALE_SMONTHNAME6 { long name for June } 
LOCALE_SMONTHNAME7 { long name for July } 
LOCALE_SMONTHNAME8 { long name for August } 
LOCALE_SMONTHNAME9 { long name for September } 
LOCALE_SMONTHNAME10 { long name for October } 
LOCALE_SMONTHNAME11 { long name for November } 
LOCALE_SMONTHNAME12 { long name for December } 
LOCALE_SMONTHNAME13 { long name for 13th month (if exists) } 
LOCALE_SABBREVMONTHNAME1 { abbreviated name for January } 
LOCALE_SABBREVMONTHNAME2 { abbreviated name for February } 
LOCALE_SABBREVMONTHNAME3 { abbreviated name for March } 
LOCALE_SABBREVMONTHNAME4 { abbreviated name for April } 
LOCALE_SABBREVMONTHNAME5 { abbreviated name for May } 
LOCALE_SABBREVMONTHNAME6 { abbreviated name for June } 
LOCALE_SABBREVMONTHNAME7 { abbreviated name for July } 
LOCALE_SABBREVMONTHNAME8 { abbreviated name for August } 
LOCALE_SABBREVMONTHNAME9 { abbreviated name for September } 
LOCALE_SABBREVMONTHNAME10 { abbreviated name for October } 
LOCALE_SABBREVMONTHNAME11 { abbreviated name for November } 
LOCALE_SABBREVMONTHNAME12 { abbreviated name for December } 
LOCALE_SABBREVMONTHNAME13 { abbreviated name for 13th month (if exists) } 
LOCALE_SPOSITIVESIGN { positive sign } 
LOCALE_SNEGATIVESIGN { negative sign } 
LOCALE_IPOSSIGNPOSN { positive sign position } 
LOCALE_INEGSIGNPOSN { negative sign position }
LOCALE_IPOSSYMPRECEDES { mon sym precedes pos amt } 
LOCALE_IPOSSEPBYSPACE { mon sym sep by space from pos amt } 
LOCALE_INEGSYMPRECEDES { mon sym precedes neg amt } 
LOCALE_INEGSEPBYSPACE { mon sym sep by space from neg amt } 
LOCALE_FONTSIGNATURE { font signature } 
LOCALE_SISO639LANGNAME { ISO abbreviated language name } 
LOCALE_SISO3166CTRYNAME { ISO abbreviated country name }
*)

